<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>6.1</title>
</head>

<body>
    <!-- <ul id="app">
        <li v-for="(item,index) in items" :key="item.message">
            {{UserName}}:{{index}}-{{item.message}}
        </li>
    </ul> -->
    <div id="app">
        <form>
            <input type="text" v-model="newItem" placeholder="Please say something">
            <input type="button" value="添加" @click="addItems">
        </form>
        <ul>
            <do-item v-for="(item,index) in items" v-bind:key="item.id" v-bind:title="item.title"
                v-on:remove="items.splice(index,1)">
            </do-item>
        </ul>
    </div>

    <script src='./vue.js'></script>

    <script>
        Vue.component('do-item', {
            props: ['title'],
            template: '\
    <li>\
      {{ title }}\
      <button v-on:click="$emit(\'remove\')">Remove</button>\
    </li>\
  '
        })
        var app = new Vue({
            el: '#app',
            data: {
                newItem: '',
                UserName: 'UserName',
                items: [
                    {
                        id: 1,
                        title: 'joker',
                    },
                    {
                        id: 2,
                        title: 'fox',
                    },
                ],
                NextId: 3
            },
            methods: {
                addItems: function () {
                    console.log(this.newItem);
                    this.items.push({
                        id: this.NextId++,
                        title: this.newItem
                    })
                    this.newItem = ''
                }
            }
        })
    </script>
</body>

</html>